package mystackgather;

import java.util.Arrays;

public class MyStack {
    public int[] elements;
    public int usedSize;

    public  MyStack(){
        this.elements = new int[5];
    }

    public void push(int val){
        if(isFull()){
            //如果数组满了，就扩容
            this.elements = Arrays.copyOf(this.elements,elements.length *2);
        }
        this.elements[this.usedSize] = val;
        this.usedSize++;
    }
    // 判断是否数组是否存满
    private  boolean isFull(){
        return  this.usedSize == this.elements.length;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈为空！");
        }
        usedSize--;
        int oldVal = this.elements[usedSize];
        return  oldVal;
    }
    // 判断栈是否为空
    public boolean isEmpty(){
        return this.usedSize == 0;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("栈为空！");
        }
        return this.elements[usedSize-1];
    }
}
